﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;

using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.win2dEffectControl.DataClass;
using 纸片人.win2dEffectControl.DataClass.PixelShaderEffectDataClass;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{

    [LastModified("2022-5-12", "create GradienMappingEffectPage.xaml")]
    [PageType("gpu", "渐变映射")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class GradienMappingEffectPage : Page
    {
        GradienMappingEffectData gmed = new GradienMappingEffectData();
        CanvasBitmapData cbd = new CanvasBitmapData();
        public GradienMappingEffectPage()
        {
            this.InitializeComponent();
            gmed.InputEffectData = cbd;
        }

        private async void selectImage_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".png");

            StorageFile file = await picker.PickSingleFileAsync();
            if (file == null)
                return;

            await cbd.FileToSoftwareBitmap(file);
            var effect = cbd.GetEffect(CanvasDevice.GetSharedDevice(), false);

            var cis = new CanvasImageSource(CanvasDevice.GetSharedDevice(), (float)cbd.Size.Width,(float) cbd.Size.Height, 96);

            using (var ds = cis.CreateDrawingSession(Colors.Transparent))
            {
                ds.DrawImage(effect);
            }

            oriImage.Source = cis;

            CreateGradientImage();
        }


        /// <summary>
        /// 创建渐变映射颜色表
        /// </summary>
        void CreateGradientImage()
        {
            Random r = new Random();
            int count = (int)r.Next(3, 10);
            var mapColors = new Color[count];

            for (int i = 0; i < count; i++)
            {
                Color mc = Color.FromArgb(255, (byte)r.Next(0, 256), (byte)r.Next(0, 256), (byte)r.Next(0, 256));
                mapColors[i] = mc;
            }



            CanvasImageSource cis = new CanvasImageSource(CanvasDevice.GetSharedDevice(), 255, 1,96);
            using (var ds = cis.CreateDrawingSession(Colors.Transparent))
            {
                var canvasGradienStop = new CanvasGradientStop[mapColors.Length];
                for (int i = 0; i < mapColors.Length; i++)
                {
                    canvasGradienStop[i] = new CanvasGradientStop
                    {
                        Color = mapColors[i],
                        Position = (float)i / (float)(mapColors.Length - 1)
                    };
                }
                CanvasLinearGradientBrush linearBrush = new CanvasLinearGradientBrush(cis.Device, canvasGradienStop)
                {
                    StartPoint = new Vector2(0, 0),
                    EndPoint = new Vector2(256, 0),
                };
                ds.FillRectangle(0, 0, 256, 1, linearBrush);
            }
            image2.Source = cis;

            gmed.MapColors = mapColors;
            var effect = gmed.GetEffect(CanvasDevice.GetSharedDevice(),false);
            var cis2 = new CanvasImageSource(CanvasDevice.GetSharedDevice(), (float)gmed.Size.Width, (float)gmed.Size.Height, 96);

            if(effect != null)
            {
                using (var ds = cis2.CreateDrawingSession(Colors.Transparent))
                {
                    ds.DrawImage(effect);
                }
            }
            effectImage.Source = cis2;

          //  var cb = (CanvasBitmap)cbd.GetEffect(CanvasDevice.GetSharedDevice(), true);
            

          //var ccs =  PictureFilter.GradientMappingEffect(cb.GetPixelColors(),(int)cb.SizeInPixels.Width,(int)cb.SizeInPixels.Height,mapColors);

          //  Function.InitialImage(ccs, (int)cbd.Size.Width,(int) cbd.Size.Height,cpueffectImage);
        }

        private void createMapping_Click(object sender, RoutedEventArgs e)
        {
            CreateGradientImage();
        }
    }
}
